// xc_lshop/shop/order/inquiry.js
// var actbill = require('../model/order_bill')
// var actbill = require('../model/order_inquirybill')
var actbill = require('../model/order_inpuirybill')

Page({
  mixins: [require('../../../xc/mixins/basicMixin')],

  /**
   * 页面的初始数据
   */
  data: {
    navid:0,
    nav:[
      {id:0,name:"全部"},
      {id:-1,name:"待付订"},
      {id:1,name:"询价中"},
      {id:2,name:"待付款"},
      {id:4,name:"已成交"},
      {id:-2,name:"已取消"},
      // {id:-3,name:"已拒绝"},
      {id:-100,name:"已退款"},
    ],
    selleruid:-1,//-1 买家 1卖家身份
  },
  xcloaddata: function (options, config) {
    console.log("config.warehouse") 
    console.log(config.wholesale) 
    if(config.wholesale){
       if(!config.wholesale.warehouse){
        this.error("批发后台-系统配制-仓库配制 未配制")
       }else{
        this.setData({
          warehouseconfig:config.wholesale.warehouse
        })
       }
       
    }

    if(options.selleruid && options.selleruid==1){
      this.setData({
        selleruid:1
      })
    }

    // var strwhere = {}
    // // strwhere["status"] = 1
    // var app = getApp();
    // var that = this;
    // app.xc_sys_config.authop.oppower("login").then(function (res) {
    //   if (res.status == 1) {
    //     console.log(res)
    //     // strwhere['buyfromuid']=
        
    //   }
    // })
  },
  op(e){
    var opname= e.currentTarget.dataset.opname;
    var item= e.currentTarget.dataset.item;

    var that=this;
    actbill.orderop(opname,item.id).then(function (result) {
       console.log("result")
       console.log(result)
       if(result['status']==1){
        wx.showToast({
          title: '操作成功',
        })
        that.getpagelist(that, 1)
       }else{
         wx.showModal({
           title: '错误',
           content:result['message'],
           complete: (res) => {
             
           }
         })
       }

    })
  },
  pay(e){
    var opname= e.currentTarget.dataset.opname;
    var item= e.currentTarget.dataset.item;

    var that=this;
    actbill.orderpay(opname,item.id).then(function (result) {
  
       if(result['status']==1){
         var pay_params=result['obj']['pay_params']
        wx.requestPayment({
          timeStamp: pay_params["timeStamp"],
          nonceStr: pay_params["nonceStr"],
          package: pay_params["package"],
          signType: pay_params['signType'],
          paySign: pay_params["paySign"],
          success(res) {
          
            order_bill.queryorder(item.id,(item.status==-1?1:4)).then(function (queryreslut) {
              if (queryreslut["status"] == 1) {
                wx.showToast({
                  title: '支付成功',
                })
                that.getpagelist(that,1)
              }
            })
          },
          fail(res) {
            if (res["errMsg"] == "requestPayment:fail cancel") {
              wx.showModal({
                title: '订单未支付',
                content: '订单未支付',
                showCancel: false,
                success(res) {

                }
              })
            }
          }
        })
       }else if(result['status']==2){
         wx.showToast({
           title: '支付成功',
         })
        that.getpagelist(that,1)
       }else{
         wx.showModal({
           title: '错误',
           content:result['message'],
           complete: (res) => {
             
           }
         })
       }

    })
  },
  //已登入传参数
  afterlogin: function (options, config, userinfo) {
    var that = this;
    if (userinfo.memberinfo && userinfo.memberinfo.id >0 ) {
      that.setData({
        "member": userinfo
      })
      that.getpagelist(that, 1)
    } else {
      that.error("用户未登录", true)
    }
  },
  getpagelist(that, rest, strwhere) {
    if (rest == 1) {
      if (!strwhere) {
        strwhere = {}
      }
      if(that.data.selleruid==1){
        strwhere['selleruid'] = that.data.member.memberinfo.id;
      }
      strwhere["opstatus"] = this.data.navid
      that.setData({
        modellist: [],
        isempty: false
      });
    }
    actbill.offset(that, rest, strwhere).then(function (result) {
      var updatemdoel = {};
      if ((result["code"] == "over" && result["obj"]) || result["code"] == "loadmore") {
        if (result["obj"]["modellist"].length < 1) {
          updatemdoel["isempty"] = true
        } else if (rest == 1) {
          updatemdoel["modellist"] = result["obj"]["modellist"];
        } else {
          if (result["obj"]["modellist"].length > 0) {
            updatemdoel["modellist"] = that.data.modellist.concat(result["obj"]["modellist"]);
          }
        }
      }
      // that.selectComponent("#ordercomment").showmodel( updatemdoel["modellist"][0],"order")      
      wx.stopPullDownRefresh();
      console.log("updatemdoel")
      console.log(updatemdoel)

      that.setData(updatemdoel);
    }, function () {

    });
  },
  onChangeNav(e){
    console.log(e)
    this.setData({
      navid:e.detail.name
    })
    var that=this;
    that.getpagelist(that,1)
  },
  ontimeFinish(){
    var that=this;
    that.getpagelist(that,1)
  },
  onReachBottom: function (e) {
    var that = this;
    this.getpagelist(that, -1);
  },
  
})